package com.example.demo.service;

import com.example.demo.mapper.CustomerMapper;
import com.example.demo.pojo.Customer;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

@Service
public class CustomerService {
    @Autowired
    public CustomerMapper customerMapper;

    public Customer select(String username,String password){
        if("".equals(username)||null==username||"".equals(password)||null==password){
            return null;
        }
        Example example=new Example(Customer.class);
        example.createCriteria()
                .andEqualTo("username",username)
                .andEqualTo("password",password);
        List<Customer> customers = customerMapper.selectByExample(example);
        if(customers==null||customers.size()<=0){
            return null;
        } return customers.get(0);
    }

    public Customer select(int id){
        Customer  customer=customerMapper.selectByPrimaryKey(id);
        return customer;
    }

    public void insert(Customer customer){
        customerMapper.insert(customer);
    }

    public void delete(int id){
        customerMapper.deleteByPrimaryKey(id);
    }

    public void update(Customer customer){
        customerMapper.updateByPrimaryKey(customer);
    }

    public PageInfo findByPage(int size, int page){
        PageHelper.startPage(page,size);//分页
        List<Customer> customers = customerMapper.selectAll();//查询指定页码的数据

        PageInfo PageInfo = new PageInfo<>(customers);//将数据封装
        return PageInfo;
    }
}
